package xpn.platform.modules.workflow.runtime.instance;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.rest.core.config.Projection;
import xpn.platform.common.repository.XpnRepository;
import xpn.platform.modules.workflow.definition.process.Process;

import java.util.Date;

@RepositoryRestResource(path = "workflow_runtime_instances", excerptProjection = InstanceDefaultProjection.class)
public interface InstanceDAO extends XpnRepository<Instance, Long> {

    /**
     * 删除权限控制
     *
     * @param entity
     */
    @RequiresPermissions("workflow.admin")
    @Override
    void delete(Instance entity);

    Instance findFirstByOldId(String oldId);
}

/**
 * 列表投影，用于返回关联属性
 *
 * @author duwei
 */
@Projection(name = "default", types = {Instance.class})
interface InstanceDefaultProjection {
    Long getId();

    Process getProcess();

    String getStarterId();

    Date getCreatedTime();
}

@Projection(name = "list", types = {Instance.class})
interface InstanceListProjection {

    Long getId();
    @Value("#{target.process != null ? target.process.id : null }")
    Long getProcessId();

    @Value("#{target.process != null ? target.process.name : null }")
    String getProcessName();

    String getStarterId();

    Date getCreatedTime();
}